﻿using ZeroBlog.Core.Domains.Blog;
using ZeroSum.Domain.Repositories;

namespace ZeroBlog.Core.DomainServices.Posts;

public class PostSeedProvider : IDataSeedProvider
{
    private readonly IRepository<Post> _repository;
    private readonly IRepository<Category> _categoryRepository;

    public PostSeedProvider(IRepository<Post> repository, IRepository<Category> categoryRepository)
    {
        _repository = repository;
        _categoryRepository = categoryRepository;
    }

    public async Task SeedAsync(IServiceProvider serviceProvider)
    {
        if (await _repository.CountAsync() <= 0)
        {
            var categories = await _categoryRepository.GetListAsync(x => !x.IsDeleted);

            var articles = new List<Post>();

            for (var i = 0; i < 50; i++)
            {
                articles.Add(new Post()
                {
                    Title = "破局者”未来矿场：撬动矿霸版图 开创“双挖矿”新纪元",
                    Content = "FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。FM上线，只是真正的开始。",
                    CoverImgUrl = "https://img.huxiucdn.com/article/cover/202201/19/194744836901.jpg",
                    Category = categories.FirstOrDefault()
                });
            }

            await _repository.AddRangeAsync(articles);
            await _repository.SaveChangesAsync();
        }
    }

    public int Order { get; set; } = 3;
}